package com.it.jzs.controller.admin;

import com.it.jzs.dao.admin.AdminBlogMapper;
import com.it.jzs.pojo.Blog;
import com.it.jzs.service.admin.AdminBlogService;
import com.it.jzs.vo.Result;
import com.it.jzs.vo.params.UpdateBlogParams;
import com.it.jzs.vo.params.WriteParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("admin")
@CrossOrigin
@Slf4j
public class AdminBlogController {

    @Resource
    private AdminBlogMapper adminBlogMapper;
    @Resource
    private AdminBlogService adminBlogService;

    @PostMapping("blog/write")
    public Result<Object> write(@RequestBody WriteParams writeParams) {   // 写文章
//        System.out.println("writeParams = " + writeParams);
        Result<Object> result = adminBlogService.insert(writeParams);
//        List<Integer> tagIds = writeParams.getTagIds();
        return Result.success(null, "测试");
    }

    @GetMapping("blog")   // 分页查询后台博客的数量
    public Result<Object> ListBlogPage(@RequestParam(value = "index") Integer index, @RequestParam(value = "size") Integer size, @RequestParam("keyWords") String keyWords) {
        Result<Object> result = adminBlogService.selectPage(index, size, keyWords);
//        System.out.println("index = " + index);
//        System.out.println("keyWords = " + keyWords);
//        System.out.println("size = " + size);
//        return Result.success(null, null);
        return result;
    }

    @DeleteMapping("blog/{id}")
    public Result<Object> deleteBlog(@PathVariable("id") Integer id) {
        //        System.out.println("id = " + id);
        Result<Object> result = adminBlogService.delete(id);
        return result;
    }

    @GetMapping("blog/{id}")
    public Result<Object> selectBlogById(@PathVariable("id") Integer id) {
//        System.out.println("id = " + id);
        Result<Object> result = adminBlogService.selectBlogById(id);
        return result;
    }

    @PutMapping("blog")  // 修改博客的接口
    public Result<Object> updateBlog(@RequestBody UpdateBlogParams updateBlogParams) {
//        System.out.println("updateBlogParams = " + updateBlogParams);
//        return Result.success(null, "测试");
//        log.info(updateBlogParams.getf);
//        System.out.println("updateBlogParams = " + updateBlogParams);
        Result<Object> result = adminBlogService.update(updateBlogParams);
        return result;
//        return Result.success(null, null);

    }

    @PostMapping("blog/deletebatch")
    public Result<Object> deleteBatch(@RequestBody List<Integer> ids) {  // 批量删除
        Result<Object> result = adminBlogService.deleteBatch(ids);

        return result;
    }


}
